Building a 3D Game with LibGDX by Sebastian Di Giuseppe & Andreas Krühlmann & Rijnswou. Elmar van

Building a 3D Game with LibGDX by Sebastian Di Giuseppe & Andreas Krühlmann & Rijnswou. Elmar van

Author:Sebastian Di Giuseppe & Andreas Krühlmann & Rijnswou. Elmar van [Giuseppe, Sebastian Di]
Language: eng
Format: azw3
Publisher: Packt Publishing
Published: 2016-08-29T04:00:00+00:00


Screens

We already have a screen that renders the game, and all it needs to work and have gameplay. Now, we will need to add other common screens (unless you go with an original design). For our game, we'll need: a main menu screen, a loading screen (to avoid weird freezes), and a leaderboards screen.

Main menu screen

Our main menu screen needs to have some basic items such as a background, a title, a play button, a leaderboards button, and a quit button. We won't get into more details because we are building a prototype so we'll only include the primary features.

We will first change Core.java to make it read our main menu screen before launching the actual game screen:

Core.java to make it read our main menu screen before launching the actual game screen: public class Core extends ApplicationAdapter { ... @Override public void create() { ... setScreen(new MainMenuScreen(this)); } ... }

Let's create a class called MainMenuScreen.java and place it on our screens package:

public class MainMenuScreen implements Screen { Core game; Stage stage; Image backgroundImage, titleImage; TextButton playButton, leaderboardsButton, quitButton; public MainMenuScreen(Core game) { this.game = game; stage = new Stage(new FitViewport(Core.VIRTUAL_WIDTH, Core.VIRTUAL_HEIGHT)); setWidgets(); configureWidgers(); setListeners(); Gdx.input.setInputProcessor(stage); } private void setWidgets() { backgroundImage = new Image(new Texture(Gdx.files.internal("data/backgroundMN.png"))); titleImage = new Image(new Texture(Gdx.files.internal("data/title.png"))); playButton = new TextButton("Play", Assets.skin); leaderboardsButton = new TextButton("Leaderboards", Assets.skin); quitButton = new TextButton("Quit", Assets.skin); } private void configureWidgers() { backgroundImage.setSize(Core.VIRTUAL_WIDTH, Core.VIRTUAL_HEIGHT); titleImage.setSize(620, 200); titleImage.setPosition(Core.VIRTUAL_WIDTH / 2 - titleImage.getWidth() / 2, Core.VIRTUAL_HEIGHT / 2); playButton.setSize(128, 64); playButton.setPosition(Core.VIRTUAL_WIDTH / 2 - playButton.getWidth() / 2, Core.VIRTUAL_HEIGHT / 2 - 100); leaderboardsButton.setSize(128, 64); leaderboardsButton.setPosition(Core.VIRTUAL_WIDTH / 2 - playButton.getWidth() / 2, Core.VIRTUAL_HEIGHT / 2 - 170); quitButton.setSize(128, 64); quitButton.setPosition(Core.VIRTUAL_WIDTH / 2 - playButton.getWidth() / 2, Core.VIRTUAL_HEIGHT / 2 - 240); stage.addActor(backgroundImage); stage.addActor(titleImage); stage.addActor(playButton); stage.addActor(leaderboardsButton); stage.addActor(quitButton); } private void setListeners() { playButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.setScreen(new GameScreen(game)); } }); leaderboardsButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.setScreen(new LeaderboardsScreen(game)); } }); quitButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Gdx.app.exit(); } }); } @Override public void render(float delta) { /** Updates */ stage.act(delta); /** Draw */ stage.draw(); } @Override public void resize(int width, int height) { stage.getViewport().update(width, height); } @Override public void dispose() { stage.dispose(); } // empty methods from Screen }

We'll start implementing the Screen interface on our class, because, as you know, this is a screen. We will save a Core instance to be a global variable, then create a new Stage object that will contain our new widgets for this respective screen, which is a backgroundImage, a titleImage, a playButton, a leaderboardsButton, and a quitButton.

Let's create a constructor for the class and have a Core game parameter to pass by our game instance, then save it. We'll instantiate the Stage object with a FitViewport again, with our static VIRTUAL_WIDTH and VIRTUAL_HEIGHT on the Core class.

The SetWidgets() method again will instantiate our widgets with our resources (skin, images, and font). Configure your widgets on the respective method.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.